package com.csw.android.player.ui.full

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
import android.os.Bundle
import android.view.View
import androidx.annotation.IntDef
import com.csw.android.dev_utils.ui.databinding.DataBindingActivity
import com.csw.android.player.PlayerManager
import com.csw.android.player.R
import com.csw.android.player.databinding.PlayerUiFullScreenPlayBinding


class FullScreenPlayActivity : DataBindingActivity<PlayerUiFullScreenPlayBinding>() {

    companion object {

        /**
         * 开启页面
         *
         * @param context 上下文
         * @param playId 播放器id
         * @param orientation 屏幕方向默认[SCREEN_ORIENTATION_UNSPECIFIED]
         */
        fun open(
            context: Context,
            playId: String?,
            orientation: Int?,
        ) {
            val intent = Intent(context, FullScreenPlayActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            intent.putExtra("playId", playId)
            orientation?.let {
                intent.putExtra("orientation", it)
            }
            context.startActivity(intent)
            (context as? Activity)?.run {
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        intent.getIntExtra("orientation", SCREEN_ORIENTATION_UNSPECIFIED).let {
            requestedOrientation = it
        }
        super.onCreate(savedInstanceState)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.getIntExtra("orientation", SCREEN_ORIENTATION_UNSPECIFIED)?.let {
            requestedOrientation = it
        }
        handleIntent(intent)
    }

    override fun getContentViewID() = R.layout.player_ui_full_screen_play

    override fun initData() {
        super.initData()
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent?){
        val playId = intent?.getStringExtra("playId")
        if (playId != null) {
            dataBinding?.videoView?.run {
                PlayerManager.getPlayer(playId).playerHolder = this
            }
        }
    }

    override fun overridePendingTransition(enterAnim: Int, exitAnim: Int) {
        super.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
    }
}